package com.sxt.system.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sxt.system.constast.Constast;
import com.sxt.system.domain.Permission;
import com.sxt.system.service.IPermissionService;
import com.sxt.system.utils.DataGridView;
import com.sxt.system.utils.ResultObj;
import com.sxt.system.utils.TreeNode;
import com.sxt.system.vo.PermissionVO;

/**
 * 	菜单管理前端控制器
 * @author Administrator
 *
 */
@RestController
@RequestMapping("menu")
public class MenuController {

	private Log log = LogFactory.getLog(MenuController.class);
	
	@Autowired
	private IPermissionService permissionService;
	
	/**
	 * 	全查询
	 */
	@RequestMapping("loadAllMenu")
	public DataGridView loadAllMenu(PermissionVO permissionVO) {
		permissionVO.setType(Constast.TYPE_MENU);//只查询菜单
		return this.permissionService.loadAllPermission(permissionVO);
	}
	
	/**
	 * 生成json树
	 */
	@RequestMapping("loadAllMenuTreeJson")
	public DataGridView loadAllMenuTreeJson(PermissionVO permissionVO) {
		permissionVO.setAvailable(Constast.AVAILABLE_TRUE);//只查询可用
		permissionVO.setType(Constast.TYPE_MENU);
		List<Permission> allPermission = this.permissionService.queryAllPermissionForList(permissionVO);
	
		List<TreeNode> treeNodes = new ArrayList<>();
		for (Permission permission : allPermission) {
			Integer id = permission.getId();
			Integer pid = permission.getPid();
			String title = permission.getTitle();
			Boolean spread = permission.getOpen()==1?true:false;
			treeNodes.add(new TreeNode(id, pid, title, spread));
		}
		return new DataGridView(treeNodes);
	}
	
	/**
	 * 	加载部门最大的排序码
	 */
	@RequestMapping("loadPermissionMaxOrderNum")
	public DataGridView loadPermissionMaxOrderNum() {
		Integer maxOrderNum = this.permissionService.queryPermissionMaxOrderNum();
		return new DataGridView(maxOrderNum+1);
	}
	
	/**
	 * 	添加
	 */
	@RequestMapping("addMenu")
	public ResultObj addMenu(Permission permission ,HttpSession session) {
		permission.setType(Constast.TYPE_MENU);
		try {
			permissionService.addPermission(permission);
			return ResultObj.ADD_SUCCESS;
		} catch (Exception e) {
			log.error("添加失败");
			return ResultObj.ADD_ERROR;
		}
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("updateMenu")
	public ResultObj updateMenu(Permission permission) {
		try {
			permissionService.updatePermission(permission);
			return ResultObj.UPDATE_SUCCESS;
		} catch (Exception e) {
			log.error("修改失败");
			return ResultObj.UPDATE_ERROR;
		}
	}
	
	/**
	 * 	根据部门ID查询当前部门有多少子节点
	 */
	@RequestMapping("checkCurrentMenuHasChild")
	public DataGridView checkCurrentMenuHasChild(Integer id) {
		Integer count = this.permissionService.queryPermissionCountByPid(id);
		return new DataGridView(count);
	}
	
	/**
	 * 	删除
	 */
	@RequestMapping("deleteMenu")
	public ResultObj deleteMenu(Integer id) {
		try {
			permissionService.removeById(id);
			return ResultObj.DELETE_SUCCESS;
		} catch (Exception e) {
			log.error("删除失败");
			return ResultObj.DELETE_ERROR;
		}
	}
}
